<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择活动类型" clearable filterable>
          <el-option v-for="item in activityTypeList" :key="item.id" :label="item.activityName" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="商品id" prop="productId">
        <el-input v-model="queryParams.productId" placeholder="请输入商品id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="库存数量" prop="stockRange">
        <el-row :gutter="10">
          <el-col :span="11">
            <el-input v-model="queryParams.params.beginStock" placeholder="开始值" clearable
              @keyup.enter.native="handleQuery" />
          </el-col>
          <el-col :span="2" style="text-align: center; line-height: 40px;">-</el-col>
          <el-col :span="11">
            <el-input v-model="queryParams.params.endStock" placeholder="结束值" clearable
              @keyup.enter.native="handleQuery" />
          </el-col>
        </el-row>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['activity:sale:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['activity:sale:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['activity:sale:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['activity:sale:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="saleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="抢购活动id" align="center" prop="id" />
      <el-table-column label="活动类型" align="center">
        <template slot-scope="scope">{{ formatActivityName(scope.row) }}</template>
      </el-table-column>
      <el-table-column label="商品id" align="center" prop="productId" />
      <el-table-column label="商品名" align="center">
        <template slot-scope="scope">{{ scope.row.product ? scope.row.product.name : scope.row.productId }}</template>
      </el-table-column>
      <el-table-column label="抢购价格" align="center" prop="flashPrice" />
      <el-table-column label="活动库存" align="center" prop="stock" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:sale:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['activity:sale:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改限时抢购对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择活动类型" clearable filterable>
            <el-option v-for="item in activityTypeList" :key="item.id" :label="item.activityName" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品id" />
        </el-form-item>

        <el-form-item label="抢购价格" prop="flashPrice">
          <el-input v-model="form.flashPrice" placeholder="请输入抢购价格" />
        </el-form-item>

        <el-form-item label="活动库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入活动库存" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSale,
    getSale,
    delSale,
    addSale,
    updateSale
  } from "@/api/activity/sale"
  import {
    listType
  } from "@/api/activity/type"

  export default {
    name: "Sale",
    data() {
      return {
        loading: true,
        ids: [],
        single: true,
        multiple: true,
        showSearch: true,
        total: 0,
        saleList: [],
        title: "",
        open: false,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: null,
          productId: null,
          params: {
            beginStock: null,
            endStock: null
          }
        },
        form: {},
        rules: {
          type: [{
            required: true,
            message: "活动类型不能为空",
            trigger: "blur"
          }],
          productId: [{
            required: true,
            message: "商品id不能为空",
            trigger: "blur"
          }],
          flashPrice: [{
            required: true,
            message: "抢购价格不能为空",
            trigger: "blur"
          }],
          stock: [{
            required: true,
            message: "活动库存不能为空",
            trigger: "blur"
          }],
        },
        activityTypeList: [],
      }
    },
    created() {
      this.getActivityTypeList()
      this.getList()
    },
    methods: {
      getActivityTypeList() {
        listType().then(res => {
          this.activityTypeList = res.rows
        })
      },
      getList() {
        this.loading = true
        listSale(this.queryParams).then(res => {
          this.saleList = res.rows
          this.total = res.total
          this.loading = false
        })
      },
      cancel() {
        this.open = false
        this.reset()
      },
      reset() {
        this.form = {
          id: null,
          type: null,
          productId: null,
          flashPrice: null,
          stock: null,
          del: null
        }
        this.resetForm("form")
      },
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      resetQuery() {
        this.resetForm("queryForm")
        // 手动清空库存区间
        this.queryParams.params = {
          beginStock: null,
          endStock: null
        };
        this.handleQuery()
      },
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加限时抢购"
      },
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getSale(id).then(res => {
          this.form = res.data
          this.open = true
          this.title = "修改限时抢购"
        })
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            const request = this.form.id != null ? updateSale(this.form) : addSale(this.form)
            request.then(() => {
              this.$modal.msgSuccess(this.form.id != null ? "修改成功" : "新增成功")
              this.open = false
              this.getList()
            })
          }
        })
      },
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm(`是否确认删除限时抢购编号为"${ids}"的数据项？`).then(() => delSale(ids)).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      handleExport() {
        this.download('activity/sale/export', {
          ...this.queryParams
        }, `sale_${new Date().getTime()}.xlsx`)
      },
      formatActivityName(row) {
        const type = this.activityTypeList.find(item => item.id === row.type)
        return type ? type.activityName : row.type
      }
    }
  }
</script>
